package com.chao.demo.test.ThreadImplements;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 实现线程的四种方法
 * 继承Thread
 * 实现Runnable
 * 实现Callable
 * 使用Executors工具类创建连接池
 */
public class ImplementsCallable implements Callable<Integer> {

    public static void main(String[] args) {
        // 2. 以 ImplementsCallable 为参数创建 FutureTask 对象
        // 3. 将 FutureTask 作为参数创建 Thread 对象
        FutureTask<Integer> futureTask = new FutureTask<Integer>(new ImplementsCallable());
        Thread thread = new Thread(futureTask);
        // 4. 调用线程对象的 start()方法
        thread.start();
        try {
            Thread.sleep(1000);
            System.out.println("返回结果 " + futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " main() 方法执行完成");
    }

    //  1. 创建实现 Callable 接口的类 ImplementsCallable
    @Override
    public Integer call() throws Exception {
        System.out.println(java.lang.Thread.currentThread().getName() + " call() 方法执行中...");
        return 1;
    }
}
